/**
 * 
 */
package com.xb.cloud.merchantapi.adapter;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.xb.cloud.gateway.client.Adapter;
import com.xb.cloud.gateway.client.AdapterContext;
import com.xb.cloud.gateway.client.ReturnCodes;
import com.xb.cloud.gateway.client.ReturnUtils;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.merchantapi.product.service.ProductSkuManageApiService;
import com.xb.cloud.product.merchant.service.client.input.bo.ProductSkuInputBo;
import com.xb.cloud.product.merchant.service.client.input.bo.ProductSkuPicInputBo;

/**
 * sku管理
 * 
 * @author kexue
 *
 */
@Component
public class ProductSkuManageAdpater extends Adapter {

	private static final Logger logger = LoggerFactory.getLogger(ProductSkuManageAdpater.class);

	@Autowired
	private ProductSkuManageApiService productSkuManageApiService;

	/**
	 * 
	 */
	public RtnInfo execute(String urlPath, Boolean isLogined, Map<String, String> bizInfo, AdapterContext context) {
		RtnInfo rtnInfo = null;

		/**
		 * 1. 在售，仓库中状态查询列表
		 */
		if ("/h5/merchant/skuManage/getSkuListByIsCanSale".equalsIgnoreCase(urlPath)) {
			Integer pageNum = null;
			Integer pageSize = null;
			Byte isCanSale = null;
			/**
			 * 参数验证
			 */
			try {
				pageNum = Integer.parseInt(bizInfo.get("pageNum"));
				pageSize = Integer.parseInt(bizInfo.get("pageSize"));
				isCanSale = Byte.parseByte(bizInfo.get("isCanSale"));
				if (pageNum == null || pageSize == null || isCanSale == null) {
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:" + bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}

			// 调取Api service
			Long currentMerchantUserId = context.getCurrentMerchantUserId();
			rtnInfo = productSkuManageApiService.getSkuListByIsCanSale(currentMerchantUserId, isCanSale, pageNum,
					pageSize);
		} else if ("/h5/merchant/skuManage/skuDown".equalsIgnoreCase(urlPath)) {
			/**
			 * 2. 下架
			 */
			Long skuId = null;
			/**
			 * 参数验证
			 */
			try {
				skuId = Long.parseLong(bizInfo.get("skuId"));
				if (skuId == null) {
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:" + bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}

			// 调取Api service
			Long currentMerchantUserId = context.getCurrentMerchantUserId();
			rtnInfo = productSkuManageApiService.skuDown(currentMerchantUserId, skuId);
		} else if ("/h5/merchant/skuManage/skuUp".equalsIgnoreCase(urlPath)) {
			/**
			 * 3. 上架
			 */
			Long skuId = null;
			/**
			 * 参数验证
			 */
			try {
				skuId = Long.parseLong(bizInfo.get("skuId"));
				if (skuId == null) {
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:" + bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			// 调取Api service
			Long currentMerchantUserId = context.getCurrentMerchantUserId();
			rtnInfo = productSkuManageApiService.skuUp(currentMerchantUserId, skuId);
		} else if ("/h5/merchant/skuManage/updateSkuPrice".equalsIgnoreCase(urlPath)){
			// 4. 价格修改	
			Long skuId = null;
			BigDecimal price=null;
			try {
				skuId = Long.parseLong(bizInfo.get("skuId"));
				price=BigDecimal.valueOf(Double.parseDouble(bizInfo.get("priceSale")));
				if (skuId == null||price==null) {
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:" + bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			// 调取Api service
			Long currentMerchantUserId = context.getCurrentMerchantUserId();
			rtnInfo = productSkuManageApiService.updatePrice(currentMerchantUserId, skuId,price);
		} else if ("/h5/merchant/skuManage/stockUpdate".equalsIgnoreCase(urlPath)) {
			//修改库存		 
			Long skuId=null;
			Long realStockNum=null;
			try{
				skuId=Long.parseLong(bizInfo.get("skuId"));
				realStockNum=Long.parseLong(bizInfo.get("realStockNum"));
				if(skuId==null||realStockNum==null||realStockNum<0)
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			} catch(Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:"+bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			
			Long currentMerchantUserId=context.getCurrentMerchantUserId();
			
			rtnInfo=productSkuManageApiService.updateStock(currentMerchantUserId, skuId, realStockNum);
		} else if ("/h5/merchant/skuManage/getStockBoBySkuId".equalsIgnoreCase(urlPath)) {
			//获取库存		 
			Long skuId=null;
			try{
				skuId=Long.parseLong(bizInfo.get("skuId"));
				if(skuId==null)
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			} catch(Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:"+bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			Long currentMerchantUserId = context.getCurrentMerchantUserId();
			rtnInfo = productSkuManageApiService.getStockBoBySkuId(currentMerchantUserId, skuId);
		} else if (urlPath.trim().toLowerCase().equalsIgnoreCase("/h5/merchant/skuManage/detail")) {
			//获取商品详情		 
			if (StringUtils.isEmpty(bizInfo.get("skuId"))) {
				return ReturnUtils.error(ReturnCodes.SKU_NUM_INVALID);
			}
			Long skuId=Long.parseLong(bizInfo.get("skuId"));
			RtnInfo skuDetail = productSkuManageApiService.getSKUDetail( skuId);
			return skuDetail;
		} else if (urlPath.trim().toLowerCase().equalsIgnoreCase("/h5/merchant/skuManage/price")) {
			//获取商品价格
			if (StringUtils.isEmpty(bizInfo.get("skuId"))) {
				return ReturnUtils.error(ReturnCodes.SKU_NUM_INVALID);
			}
			Long skuId=Long.parseLong(bizInfo.get("skuId"));
			
			RtnInfo skuDetail = productSkuManageApiService.getSKUPrice(skuId);
			return skuDetail;
		} else if (urlPath.trim().toLowerCase().equalsIgnoreCase("/h5/merchant/skuManage/delete")) {
			/**
			 * 商家删除SKU
			 */
			Long skuId = null;
			/**
			 * 参数验证
			 */
			try {
				skuId = Long.parseLong(bizInfo.get("skuId"));
				if (skuId == null) {
					return RtnInfo.ParameterErrRtnInfo("非法参数错误");
				}
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:" + bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}

			// 调取Api service
			Long currentMerchantUserId = context.getCurrentMerchantUserId();
			rtnInfo = productSkuManageApiService.skuDelete(currentMerchantUserId, skuId);
		} else if(urlPath.trim().toLowerCase().equalsIgnoreCase("/h5/merchant/skuManage/update")){
			/**
			 * 商家更新SKU
			 */
			//验证参数存在
			if (StringUtils.isEmpty(bizInfo.get("productId"))
					|| StringUtils.isEmpty(bizInfo.get("skuPics"))
					|| StringUtils.isEmpty(bizInfo.get("skuInput"))
					|| StringUtils.isEmpty(bizInfo.get("priceSale"))
					|| StringUtils.isEmpty(bizInfo.get("priceMarket"))
					|| StringUtils.isEmpty(bizInfo.get("stockNum"))){ 
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}

			Long endMerchantUserId=null;
			Long productId=null;
			List<ProductSkuPicInputBo> skuPics=null; 
			String detailContentJson=null;
			ProductSkuInputBo skuInput=null;
			BigDecimal priceSale=null;
			BigDecimal priceMarket=null; 
			Long stockNum=null; 
			 
			//验证参数格式
			try {
				productId = Long.parseLong(bizInfo.get("productId"));
				skuPics= JSONArray.parseArray(bizInfo.get("skuPics"), ProductSkuPicInputBo.class);
				detailContentJson=bizInfo.get("detailContentJson");
				skuInput=JSON.parseObject(bizInfo.get("skuInput"), ProductSkuInputBo.class);
				priceSale=new BigDecimal(bizInfo.get("priceSale"));
				priceMarket=new BigDecimal(bizInfo.get("priceMarket"));
				stockNum=Long.parseLong(bizInfo.get("stockNum"));
			} catch (Exception e) {
				e.printStackTrace();
				logger.error("非法参数错误,入参为:"+bizInfo);
				return RtnInfo.ParameterErrRtnInfo("非法参数错误");
			}
			//获取当前用户
			endMerchantUserId=context.getCurrentMerchantUserId();
			//调取Api service
			rtnInfo = productSkuManageApiService.updateProductSku(endMerchantUserId,productId,skuPics,detailContentJson, skuInput, priceSale,priceMarket, stockNum);
		}
		return rtnInfo;
	}
	
	@Override
	public String getUrlMappingPath() {
		return "/h5/merchant/skuManage/**";
	}
	
	@Override
	public boolean isCheckUserToken() {
		return true;
	}

}
